﻿#include "GraphicsPinkItem.h"
#include <GraphicsBaseItem.h>
#include <GraphicsScene.h>
#include <GraphicsView.h>
#include <GraphicsUtil.h>
#include <QCursor>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene>
//#include <QtSvg/QSvgRenderer>


GraphicsPinkItem::GraphicsPinkItem(PinkItemType type, GraphicsBaseItem* parent)
	:QGraphicsObject(nullptr)
	, m_type(type)
	, m_parentItem(parent)
{
	assert(parent);
	m_roationImage = QImage(":/images/rotation.png");
	m_cornerImage = QImage(":/images/ic_aibg_edit_shuiping.svg")
		.scaled(QSize(28, 28), Qt::KeepAspectRatio, Qt::SmoothTransformation);

	setAcceptHoverEvents(true);
	setZValue(10);
	if (m_type < PinkItemType::CornerEnd_Enum) {
		m_size = QSize(14, 14);
	}
	else if (m_type == PinkItemType::MidRight_Enum || m_type == PinkItemType::MidLeft_Enum) {
		m_size = QSize(7, 20);
	}
	else if (m_type == PinkItemType::MidTop_Enum || m_type == PinkItemType::MidBottom_Enum) {
		m_size = QSize(20, 7);
	}
	else if (m_type == PinkItemType::Rotation_Enum) {
		m_size = QSize(32, 32);
	}
	else if (m_type > PinkItemType::RotationEnd_Enum) {
		m_size = QSize(10, 10);
		setZValue(9);
	}
	hide();
	parent->scene()->addItem(this);
	connect(&(parent->getItemData()), &GraphicsBaseItemData::sigRotateAngleChanged, this, &GraphicsPinkItem::onUpdateCursor);
}

void GraphicsPinkItem::onUpdateCursor() {
	QCursor cursor;
	switch (m_type)
	{
	case PinkItemType::TopLeft_Enum:
	case PinkItemType::TopRight_Enum:
	case PinkItemType::BottomRight_Enum:
	case PinkItemType::BottomLeft_Enum:
	{
		// 缩放图标
		cursor = getCornerCursor();
	}
	break;
	case PinkItemType::MidTop_Enum:
	case PinkItemType::MidBottom_Enum:
	{
		cursor = QCursor(Qt::SizeVerCursor);
	}
	break;
	case PinkItemType::MidRight_Enum:
	case PinkItemType::MidLeft_Enum:
	{
		cursor = QCursor(Qt::SizeHorCursor);
	}
	break;
	case PinkItemType::Rotation_Enum:
	{
		// 旋转图标
		QPixmap roationPix(":/images/ic_aibg_edit_xuanzhuan.svg");
		roationPix = roationPix.scaled(32, 32, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
		cursor = QCursor(roationPix);
	}
	break;
	default:
		break;
	}
	setCursor(cursor);
}

void GraphicsPinkItem::updateParentGraphcisGeometry(const QPointF& /*delta*/)
{
	if (!m_parentItem)
		return;

	m_parentItem->operationDoing(m_type, m_lastScenePos);
	const QPointF deltaPressMove = m_lastScenePos - m_pressScenePos;
	if (m_type < PinkItemType::CornerEnd_Enum) {
		// 缩放图元
		m_parentItem->scaleGraphics(m_type, deltaPressMove);
	}
	else if (m_type > PinkItemType::CornerEnd_Enum && m_type < PinkItemType::MidEnd_Enum) {
		// 拉伸图元
		m_parentItem->stretchGraphics(m_type, deltaPressMove);
	}
	else if (m_type > PinkItemType::MidEnd_Enum && m_type < RotationEnd_Enum) {
		// 旋转图元
		m_parentItem->rotateGraphics(m_lastScenePos);
	}
}

void GraphicsPinkItem::setAngleVisable(bool isAngleVisable)
{
	m_isAngleVisable = isAngleVisable;
	update();
}

QCursor GraphicsPinkItem::getCornerCursor()
{
	if (!m_parentItem)
		return QCursor();

	QImage image = m_cornerImage;
	// 计算角度
	QLineF centerLine(m_parentItem->scenePos(), scenePos());
	// 与水平线的夹角
	QPointF endPoint(0, m_parentItem->scenePos().y());
	const qreal angle = centerLine.angleTo(QLineF(m_parentItem->scenePos(), endPoint));
	// 图片旋转
	image = GraphicsUtil::roateImage(image, angle);
	QCursor cursor(QPixmap::fromImage(image));
	return cursor;
}

void GraphicsPinkItem::onUpdatePos()
{
	if (!m_parentItem)
		return;

	GraphicsBaseItemData& itemData = m_parentItem->getItemData();
	const QSizeF parentScaleSize = itemData.getScaleSize();
	const bool isCenterOrigin = itemData.IsOriginOnCenter();

	const QSizeF scaleSize = getScaleSize();
	QPointF newPoint = QPointF(0, 0);
	switch (m_type)
	{
		// 角点
	case GraphicsPinkItem::PinkItemType::TopLeft_Enum:
		newPoint = QPointF(0, 0);
		break;
	case GraphicsPinkItem::PinkItemType::TopRight_Enum:
		newPoint = QPointF(parentScaleSize.width(), 0);
		break;
	case GraphicsPinkItem::PinkItemType::BottomRight_Enum:
		newPoint = QPointF(parentScaleSize.width(), parentScaleSize.height());
		break;
	case GraphicsPinkItem::PinkItemType::BottomLeft_Enum:
		newPoint = QPointF(0, parentScaleSize.height());
		break;

		// 中点
	case GraphicsPinkItem::PinkItemType::MidTop_Enum:
		newPoint = QPointF(parentScaleSize.width() / 2, 0);
		break;
	case GraphicsPinkItem::PinkItemType::MidRight_Enum:
		newPoint = QPointF(parentScaleSize.width(), parentScaleSize.height() / 2);
		break;
	case GraphicsPinkItem::PinkItemType::MidBottom_Enum:
		newPoint = QPointF(parentScaleSize.width() / 2, parentScaleSize.height());
		break;
	case GraphicsPinkItem::PinkItemType::MidLeft_Enum:
		newPoint = QPointF(0, parentScaleSize.height() / 2);
		break;

		// 旋转点
	case GraphicsPinkItem::PinkItemType::Rotation_Enum:
		newPoint = QPointF(parentScaleSize.width() / 2.0, -1 * scaleSize.width());
		break;

		// 连接线
	case GraphicsPinkItem::PinkItemType::LeftLine_Enum:
		newPoint = QPointF(0, parentScaleSize.height() / 2);
		m_size = QSize(1, parentScaleSize.height());
		break;
	case GraphicsPinkItem::PinkItemType::RightLine_Enum:
		newPoint = QPointF(parentScaleSize.width(), parentScaleSize.height() / 2);
		m_size = QSize(1, parentScaleSize.height());
		break;
	case GraphicsPinkItem::PinkItemType::TopLine_Enum:
		newPoint = QPointF(parentScaleSize.width() / 2.0, 0);
		m_size = QSize(parentScaleSize.width(), 1);
		break;
	case GraphicsPinkItem::PinkItemType::BottomLine_Enum:
		newPoint = QPointF(parentScaleSize.width() / 2.0, parentScaleSize.height());
		m_size = QSize(parentScaleSize.width(), 1);
		break;
	default:
		break;
	}

	if (isCenterOrigin) {
		//往左和上偏移父图元的一半尺寸
		newPoint += QPointF(-parentScaleSize.width() / 2, -parentScaleSize.height() / 2);
	}
	// 会自动将m_parentItem的旋转映射到新的坐标点
	newPoint = m_parentItem->mapToScene(newPoint);
	setPos(newPoint);
}

GraphicsPinkItem::~GraphicsPinkItem()
{

}

void GraphicsPinkItem::setItemScale(qreal /*scale*/)
{
	//m_scale = scale;
}

QRectF GraphicsPinkItem::boundingRect() const
{
	// 以中点为原点
	const QSizeF scaleSize = getScaleSize();
	const QRectF rect(-scaleSize.width() / 2 - m_penWidth / 2,
		-scaleSize.height() / 2 - m_penWidth / 2,
		scaleSize.width() + m_penWidth,
		scaleSize.height() + m_penWidth);

	return rect;
}

void GraphicsPinkItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
	const QSizeF scaleSize = getScaleSize();
	if (scaleSize.width() <= 0 || scaleSize.height() <= 0)
		return;

	GraphicsBaseItemData& itemData = m_parentItem->getItemData();
	const QSizeF parentScaleSize = itemData.getScaleSize();

	painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	painter->save();
	QPen pen(QColor("#221FF6"));
	pen.setWidth(m_penWidth);
	if (m_isHover) {
		painter->setBrush(QColor("#221FF6"));
	}
	else {
		painter->setBrush(Qt::white);
	}
	painter->setPen(pen);
	painter->rotate(itemData.getRotateAngle());
	if (m_type < PinkItemType::CornerEnd_Enum) {
		// 角点绘制
		painter->drawEllipse(-scaleSize.width() / 2, -scaleSize.height() / 2, scaleSize.width(), scaleSize.height());
	}
	else if (m_type > PinkItemType::CornerEnd_Enum && m_type < PinkItemType::MidEnd_Enum) {
		// 中点绘制
		painter->drawRoundedRect(-scaleSize.width() / 2, -scaleSize.height() / 2, scaleSize.width(), scaleSize.height(), 2, 2);
	}
	else if (m_type > PinkItemType::MidEnd_Enum && m_type < PinkItemType::RotationEnd_Enum) {
		// 绘制旋转
		if (m_isAngleVisable)
		{
			const qreal viewScale = m_parentItem->getScene()->getView()->getViewScale();
			const QSizeF newScaleSize = scaleSize / viewScale;

			// 显示角度
			painter->setPen(Qt::NoPen);
			// 背景绘制
			painter->setBrush(QColor(0, 0, 0, 100));
			painter->drawRoundedRect(QRect(-scaleSize.width() / 2, -scaleSize.height() / 2, scaleSize.width(), scaleSize.height()), 5, 5);

			// 旋转角度绘制
			painter->setPen(Qt::white);
			QFont font;
			font.setPixelSize(15);
			painter->setFont(font);
			int angle = itemData.getRotateAngle();
			if (angle < 0) {
				angle += 360;
			}
			QString roateAngle = QString("%1°").arg(angle);
			painter->drawText(boundingRect(), Qt::AlignCenter, roateAngle);
		}
		else {
			const QImage scaledImage = m_roationImage.scaled(scaleSize.toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
			painter->drawImage(-scaleSize.width() / 2, -scaleSize.height() / 2, scaledImage);
		}
	}
	else if (m_type == PinkItemType::LeftLine_Enum) {
		// 左边连接线
		painter->drawLine(QPointF(0, -parentScaleSize.height() / 2), QPointF(0, parentScaleSize.height() / 2));
	}
	else if (m_type == PinkItemType::TopLine_Enum) {
		// 顶部连接线
		painter->drawLine(QPointF(-parentScaleSize.width() / 2.0, 0), QPointF(parentScaleSize.width() / 2.0, 0));
	}
	else if (m_type == PinkItemType::RightLine_Enum) {
		// 右侧连接线
		painter->drawLine(QPointF(0, -parentScaleSize.height() / 2), QPointF(0, parentScaleSize.height() / 2));
	}
	else if (m_type == PinkItemType::BottomLine_Enum) {
		// 底部连接线
		painter->drawLine(QPointF(-parentScaleSize.width() / 2.0, 0), QPointF(parentScaleSize.width() / 2.0, 0));
	}
	painter->restore();
	// 绘制原点
	//if (m_type > PinkItemType::RotationEnd) {
	//	painter->setPen(Qt::NoPen);
	//	painter->setBrush(Qt::red);
	//	const int radius = 8;
	//	painter->drawEllipse(-radius / 2, -radius / 2, radius, radius);
	//}
}

void GraphicsPinkItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
	if (event->button() != Qt::LeftButton)
		return;
	m_lastScenePos = event->scenePos();
	m_pressScenePos = m_lastScenePos;
	if (m_parentItem) {
		m_parentItem->operationStart(m_type, m_lastScenePos);
	}
}

void GraphicsPinkItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* /*event*/)
{
	m_isControling = false;
	if (m_parentItem) {
		m_parentItem->operationDone(m_type, m_lastScenePos);
	}
}

void GraphicsPinkItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
	if (event->buttons() != Qt::LeftButton)
		return;
	// 计算鼠标移动的距离
	const QPointF delta = event->scenePos() - m_lastScenePos;
	m_isControling = true;
	// 更新上一次的位置
	m_lastScenePos = event->scenePos();
	// 通知父图元进行图像变换
	updateParentGraphcisGeometry(delta);
	QGraphicsObject::mouseMoveEvent(event);
}

void GraphicsPinkItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
	m_isHover = true;
	onUpdateCursor();
	QGraphicsObject::hoverEnterEvent(event);
}

void GraphicsPinkItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
	m_isHover = false;
	unsetCursor();
	QGraphicsObject::hoverLeaveEvent(event);
}